package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("C-端 购物车接口")
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShopCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result save(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.insert(shoppingCartDTO);
        return Result.success();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCarts = shoppingCartService.getShoppingCartsByUserId(userId);
        return Result.success(shoppingCarts);
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result deleteAllShoppingCartByUserId(){
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.deleteAllShoppingCartByUserId(userId);
        return Result.success();
    }

    @ApiOperation("购物车减一")
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.shoppingCartSub(shoppingCartDTO);
        return Result.success();
    }
}
